<?php
/**
 * User: hjd-yao
 * Date: 2019/7/31
 * Time: 10:23
 */

namespace Develop\Http\Controllers;


use App\Helpers\Search;
use App\Helpers\SelfEncrypt;
use App\Helpers\SelfResponse;
use App\Http\Controllers\Controller;
use App\Models\Shop\AdPosModel;
use Illuminate\Http\Request;

class AdPosController extends Controller
{
    public function index(Request $request)
    {
        $search = Search::make($request)->bindModel(new AdPosModel())
            ->time('created_at','创建时间')
            ->bindSearchData();

        $list = $search->where()->orderBy('client_type','asc')->paginate(20);

        return view('develop::ad.index',[
            'page_title'=>'广告位',
            'list'=>$list,
            'search_tpl'=>$search
        ]);
    }

    public function create(Request $request)
    {
        if($request->isMethod('post')){
            $info = new AdPosModel();
            $info->pos_code = SelfEncrypt::createSn('AD');
            $info->pos_name = $request->post('pos_name');
            $info->ad_width = $request->post('ad_width');
            $info->ad_height = $request->post('ad_height');
            $info->ad_number = $request->post('ad_number');
            $info->client_type = $request->post('client_type');
            $info->pos_desc = $request->post('pos_desc');
            $info->status = $request->post('status');
            if($info->save()){
                return SelfResponse::success('添加成功','d_AdPos_index');
            }
            return SelfResponse::error('添加失败,请稍后再试');
        }else{
            return view('develop::ad.create_and_edit',[
                'page_title'=>'添加位',
            ]);
        }
    }

    public function update(Request $request,$id)
    {
        if($request->isMethod('post')){
            $info = AdPosModel::find($id);
            $info->pos_name = $request->post('pos_name');
            $info->ad_width = $request->post('ad_width');
            $info->ad_height = $request->post('ad_height');
            $info->ad_number = $request->post('ad_number');
            $info->client_type = $request->post('client_type');
            $info->pos_desc = $request->post('pos_desc');
            $info->status = $request->post('status');
            if($info->save()){
                return SelfResponse::success('编辑成功','d_AdPos_index');
            }
            return SelfResponse::error('编辑失败,请稍后再试');
        }else{
            $info = AdPosModel::find($id);
            return view('develop::ad.create_and_edit',[
                'page_title'=>'添加位',
                'info'=>$info
            ]);
        }
    }

    public function switchAdPos($id)
    {
        $info = AdPosModel::find($id);
        if(!$info){
            return SelfResponse::error('操作的信息不存在');
        }
        $status = abs($info->status-1);
        $msg = $status == 1?'启用':'禁用';
        $info->status = $status;
        if($info->save()){
            return SelfResponse::success('成功'.$msg.$info->title,'d_AdPos_index');
        }
        return SelfResponse::error($info->title.$msg.'失败');
    }

    public function delete(Request $request)
    {
        $id = array_filter(str2arr($request->input('id')));
        if(!$id){
            return SelfResponse::error('请选择要删除的数据');
        }
        if(AdPosModel::whereIn('id',$id)->delete()){
            return SelfResponse::success('删除成功');
        }
        return SelfResponse::error('删除失败');
    }
}